/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.filechooser.FileFilter;

import twaver.PopupMenuGenerator;
import twaver.TView;
import twaver.TWaverUtil;
import twaver.chart.AbstractChart;

public class ChartPopupMenuGenerator implements PopupMenuGenerator {
	
	public final static ChartPopupMenuGenerator INSTANCE = new ChartPopupMenuGenerator();
	private JFileChooser chooser;
	private JFileChooser svgChooser;
	
	private ChartPopupMenuGenerator(){	
	}
	
	public JPopupMenu generate(TView tview, MouseEvent evt) {
		final AbstractChart chart = (AbstractChart)tview;
		
		JPopupMenu popMenu = new JPopupMenu();
		
		final JMenuItem exportWhole = new JMenuItem("Export Whole");
		exportWhole.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if (chooser == null) {
					chooser = TWaverUtil.createImageFileChooser();
				}
				chart.exportImage(chooser, true);
			}
		});
		popMenu.add(exportWhole);
		
		final JMenuItem exportChart = new JMenuItem("Export Center");
		exportChart.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if (chooser == null) {
					chooser = TWaverUtil.createImageFileChooser();
				}
				chart.exportImage(chooser, false);
			}
		});
		popMenu.add(exportChart);
		final JMenuItem exportSvg = new JMenuItem("Export SVG");
		exportSvg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (svgChooser == null) {
					svgChooser = new JFileChooser();
					svgChooser.setFileFilter(new FileFilter() {
						public boolean accept(File file) {
							return file.isDirectory() || file.getName().toLowerCase().endsWith(".svg");
						}
						public String getDescription() {
							return "SVG File";
						}
					});
				}
				int returnVal = svgChooser.showSaveDialog(TWaverUtil.getWindowForComponent(chart));
				if (returnVal != JFileChooser.APPROVE_OPTION) {
					return;
				}
				String fileName = svgChooser.getSelectedFile().getAbsolutePath();
				try {
					chart.exportSVG(fileName);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		popMenu.add(exportSvg);
		
		final JMenuItem reset = new JMenuItem("Reset");
		reset.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				chart.reset();
			}
		});
		popMenu.add(reset);
		
		final JCheckBoxMenuItem enableXZoom = new JCheckBoxMenuItem("Enable XZoom", chart.isEnableXZoom());
		enableXZoom.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				chart.setEnableXZoom(enableXZoom.isSelected());
			}
		});
		popMenu.add(enableXZoom);
		
		final JCheckBoxMenuItem enableYZoom = new JCheckBoxMenuItem("Enable YZoom", chart.isEnableYZoom());
		enableYZoom.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				chart.setEnableYZoom(enableYZoom.isSelected());
			}
		});
		popMenu.add(enableYZoom);
		
		final JCheckBoxMenuItem enableXTranslate = new JCheckBoxMenuItem("Enable XTranslate", chart.isEnableXTranslate());
		enableXTranslate.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				chart.setEnableXTranslate(enableXTranslate.isSelected());
			}
		});
		popMenu.add(enableXTranslate);
		
		final JCheckBoxMenuItem enableYTranslate = new JCheckBoxMenuItem("Enable YTranslate", chart.isEnableYTranslate());
		enableYTranslate.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				chart.setEnableYTranslate(enableYTranslate.isSelected());
			}
		});
		popMenu.add(enableYTranslate);
		
		
		
		return popMenu;
	}

}